package com.example.collection;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Collection;

/**
 * @Author 郅占伟
 * @ClassName TestCollection
 * @Description TODO
 * @Date 2024年10月21日
 * @Version: 1.0
 */
public class TestCollection {

    /**
     * @description Collection接口的add方法
     */
    @Test
    public void testAdd() {
        Collection<String> collection = new ArrayList<String>();
        collection.add("hello");
        collection.add("world");
        System.out.println(collection);
    }

    /**
     * @description Collection接口的remove方法，如果元素不存在，不会抛出异常
     */
    @Test
    public void testRemove() {
        Collection<String> collection = new ArrayList<String>();
        collection.add("hello");
        collection.add("world");

        collection.remove("hello");
        collection.remove("wor");
        System.out.println(collection);
    }

    /**
     * @description Collection接口的clear方法，清空集合中的所有元素
     */
    @Test
    public void testClear() {
        Collection<String> collection = new ArrayList<String>();
        collection.add("hello");
        collection.add("world");

        collection.clear();
        System.out.println(collection);
    }

    @Test
    public void testContains() {
        Collection<String> collection = new ArrayList<String>();
        collection.add("hello");
        collection.add("world");

        boolean contains = collection.contains("hello");
        System.out.println(contains);
    }

    /**
     * @description Collection接口的isEmpty方法，判断集合是否为空
     */
    @Test
    public void testIsEmpty() {
        Collection<String> collection = new ArrayList<String>();
        collection.add("hello");
        collection.add("world");

        boolean empty = collection.isEmpty();
        System.out.println(empty);
    }

    /**
     * @description Collection接口的size方法，返回集合中元素的个数
     */
    @Test
    public void testSize() {
        Collection<String> collection = new ArrayList<String>();
        collection.add("hello");
        collection.add("world");

        int size = collection.size();
        System.out.println(size);
    }

    /**
     * @description Collection接口的toArray方法，将集合中的元素转换为数组
     */
    @Test
    public void testToArray() {
        Collection<String> collection = new ArrayList<String>();
        collection.add("hello");
        collection.add("world");

        Object[] array = collection.toArray();
        for (Object obj : array) {
            System.out.println(obj);
        }
    }
}